package mystack;

import javax.sound.midi.Soundbank;
import java.sql.SQLOutput;

public class ListNodeStack {
    private DoubleListNode val;


    public ListNodeStack(String val) {
        this.val =new DoubleListNode(val);;
    }

    public boolean push(String item){
        DoubleListNode newNode = new DoubleListNode(item);

        this.val.next=newNode;
        newNode.pre=val;

        val=newNode;
        return true;
    }

    public String pop(){
        if(val!=null){
            String tmp = this.val.val;
            val=val.pre;
            val.next=null;
            return tmp;

        }
        return null;
    }

    public void print(){
        if(val==null){
            System.out.println("无数据");
        }
        System.out.print(val.val);
        while (val.hashPre()){
            System.out.print("<-");
            System.out.print(val.pre.val);
            val=val.pre;
        }
        System.out.println();
        System.out.print(val.val);
        while (val.hashNext()){
            System.out.print("->");
            System.out.print(val.next.val);
            val=val.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ListNodeStack begin = new ListNodeStack("begin");
        begin.push("stack");
        begin.push("stack1");
        begin.push("stack2");
        begin.print();
        System.out.println();
        System.out.println(begin.pop());
        begin.print();
    }
}
